<#include "../common/header.htm">
<div class="content-wrapper">
	<section class="content-header">
		<ol class="breadcrumb">
			<li><a href="${ctx}/">首页</a></li>
			<li class="active"><a href="#">企业管理</a></li>
		</ol>
	</section>
	<section class="content">
		<div class="box">
			<form action="${ctx}/company/list" method="get" id="listForm">
			<div class="box-header">
				<div class="row RowSearchWork">
					<div class="col-sm-12">
						<#if userType != 1>
						    <div class="col-sm-2 three-word">
								<label>代理商：</label> 
								<select class="form-control select2" name="agentId">
								   <option value="">请选择</option>
								   <#list agentList as a>
								        <#if listParams?? && listParams.agentId?? && a.id == listParams.agentId>
											<option value="${a.id}" selected="selected">${a.nickName}</option>
										<#else>
										     <option value="${a.id}">${a.nickName}</option>
										</#if> 
								   </#list>
								 </select>
							</div>
						</#if>
					    <div class="col-sm-2" style="width: 21%;">
							<label>所属行业：</label> 
							<select class="form-control" name="industryId">
							   <option value="">请选择</option>
							   <#list industryMap as key, value>
									<#if listParams?? && listParams.industryId?? && key == listParams.industryId?c>
										<option value="${key}" selected="selected">${value}</option>
									<#else>
										<option value="${key}">${value}</option>
									</#if> 
							   </#list>
							 </select>
						</div>
						<div  class="col-sm-4">
							<label>所属地区：</label>
							<select class="form-control select2" name="provinceCode"  style="width: 120px;" >
								<option value="">--请选择--</option>
								<#list firstLevel as a>
									<option value="${a.code}" <#if listParams?? && listParams.provinceCode?? && a.code == listParams.provinceCode>selected</#if> >${a.city}</option>
								</#list>
							</select>
							<div style="display: inline-block;">
								<select class="form-control select2" name="cityCode" id="cityCode" style="width: 120px">
									<option value="">--请选择--</option>
									<#if secondLevel??>
										<#list secondLevel as a>
											<option value="${a.code}" <#if listParams?? && listParams.cityCode?? && a.code == listParams.cityCode>selected</#if> >${a.city}</option>
										</#list>
									</#if>
								</select>
							</div>
						</div>
					</div>
					<div class="col-sm-12">
						<div class="col-sm-3">
							<input name="keyword" type="text" class="form-control" placeholder="请输入企业Id/名称/登录名" value="${listParams.keyword!}"/>
						</div>
						<div class="col-sm-1">
							<input type="button"  class="btn btn-default btn-search" onclick="companys.query()" value="查询" />
						</div>
						<div class="col-sm-1">
							<input type="button"  class="btn btn-primary" onclick="companys.form()" value="新增" />
						</div>
					</div>
				</div>
			</div>
			<div class="box-body">
				<table class="table table-hover table-bordered table-striped table-manage">
					<thead>
						<tr>
							<th style="width:10%;">序号</th>
							<#if userType == 3>
							<th style="width:10%;">企业ID</th>
							</#if>
							<th style="width:10%;">公司名称</th>
							<th style="width:10%;">所属行业</th>
							<th style="width:10%;">所属地区</th>
							<th style="width:10%;">开户时间</th>
							<th style="width:10%;">登录名</th>
							<th style="width:10%;">客户经理</th>
							<th style="width:10%;">销售</th>
							<th style="width:10%;">通道</th>
							<th style="width:10%;">并发数</th>
							<th style="width:330px;">操作</th>
						</tr>
					</thead>
					<tbody>
						<#list page.content as p>
						<tr>
							<td>${page.size * page.number + p_index+1}</td>
							<#if userType == 3>
							<td>${p.companyId!}</td>
							</#if>
							<td title="${p.companyName!}">
								<#if p.manageUserId??>
									<a href="${ctx}/company/detail?companyId=${p.companyId}">${p.companyName!}</a>
								<#else>
									${p.companyName!}
								</#if>
							</td>
							<#if p.industryId?? && industryMap["${p.industryId}"]??>
								<td>${industryMap["${p.industryId}"]!}</td>
							<#else>
								<td></td>
							</#if>
							<td>${p.address!}</td>
							<td title="${p.insertTime!}">${p.insertTime!}</td>
							<td title="${p.loginName!}">${p.loginName!}</td>
							<td>${p.accountManagerName!}</td>
							<td>${p.saleName!}</td>
							<#if p.companyId?? && channelNumMap["${p.companyId}"]?? >
								<td>${channelNumMap["${p.companyId}"]!}</td>
							<#else>
								<td>0</td>
							</#if>
							<td>${p.concurrencyNum!}</td>
							<td>
								<#if p.manageUserId??>
									<a href="${ctx}/company/detail?companyId=${p.companyId}">详情</a>
									<a href="javascript:void(0)" onclick="companys.form('${p.companyId}')">编辑</a>
									<a href="javascript:void(0)" onclick="companys.updateTheWords('${p.companyId}')">话术管理</a>
									<a href="javascript:void(0)" onclick="companys.resetPwd('${p.companyId}')">重置密码</a>
									<a href="javascript:void(0)" onclick="companys.manage('${p.companyId}')">管理</a>
									<a href="javascript:void(0)" onclick="companys.roleManage('${p.companyId}')">功能授权</a>
								<#else>
									此公司没有管理员
								</#if>
							</td>
						</tr>
						</#list>
					</tbody>
				</table>
				<#include "../common/page.htm">
			</div>
			</form>
		</div>
	</section>
</div>

<script type="text/javascript">
	$('.select2').select2();
	$.fn.modal.Constructor.prototype.enforceFocus = function() {};
	
	$("select[name='provinceCode']").change(function(){
		var provinceCode = $(this).val();
		$.ajax({
			url:"${ctx}/area/getCity?provinceCode="+provinceCode,
			dataType:"json",
			success:function(res){
				var citys = res.data;
				console.log(citys);
				$("#cityCode").find("option").remove();
				$("#cityCode").append("<option value=''>--请选择--</option>");
				for(var i=0; i<citys.length; i++){
					$("#cityCode").append("<option value='"+citys[i].code+"'>"+citys[i].city+"</option>");
				}
			}
		})
	});
	
	var win;
	var is_submit=false;
	var outPath = "${redirectUri}";
	var pmId="";
	if('${userType}' == 1) {
		pmId="${loginUser.id}";
	}
	var companys = {
		query : function() {
			$(":input[name='pageNumber']").val(0);
			$("form").submit();
		},
		form : function(id) {
			if(is_submit){
				return;
			}
			is_submit=true;
			id = id?id:"";
			var title = id?"修改企业":"新增企业";
			var j = 0;
			base.getView({
				url : "${ctx}/company/form?id="+id+"&userType=${userType}",
				success:function(result){
					bootbox.dialog({
						title:title,
						message:result,
						size:"large",
						closeButton : false,
						buttons:{
							cancel:{
								label:"取消",
								className:"btn btn-secondary",
								callback:function(){
									is_submit=false;
								}
							},
							sure:{
								label:"确定",
								className:"btn btn-primary",
								callback:function(){
									if (j != 0) {
										is_submit=false;
										return ;
									}
									j++;
									base.validAndAjaxSubmitJson($("#addCompany"), function () {
										j = 0;
									}, function (data) {
										if (data.result == 'success') {
											$("#listForm").submit();
										} else {
											j = 0;
										}
									});
									return false;
								}
							}
						}
					})
				}
			})
		},
		// 初始化话术
		addTheWords : function(companyId, obj) {
			var companyName = $(obj).attr("name");
			var title = "新增话术("+companyName+")";
			base.getView({
				url:"${ctx}/questionnaire/addTheWords?companyId="+companyId,
				success:function(result){
					bootbox.dialog({
						title:title,
						message:result,
						size:"large",
						buttons:{
							cancel:{
								label:"取消",
								className:"btn btn-secondary",
							},
							sure:{
								label:"确定",
								className:"btn btn-primary",
								callback:function(){
									copyTheWords();
									return false;
								}
							}
						}
					})
				}
			});
		},
		// 话术管理1
		updateTheWords : function(companyId) {
			var url = "${ctx}/login/sysOutLogin?_t="+Math.random();
			$.ajax({
				url : url,
				type : 'post',
				dataType : 'json',
				data : {"companyId":companyId},
				success : function(data) {
					if(data!=null){
						var sign = data.sign;
						var timestamps = data.timestamps;
						//var newUrl = outPath+"login/sysOutLogin.action?_clientType=wap&sign="+sign+"&timestamps="+timestamps+"&companyId="+companyId+"&requestType=questionnaireList&pmId="+pmId+"&fromType=1&outLoginName=${loginUser.nickName!}";
						var newUrl = outPath+"#/jump?sign="+sign+"&timestamps="+timestamps+"&companyId="+companyId+"&requestType=speechTechniqueManage";
						if(win){
							win.close();
						}
						win = window.open(newUrl,"_blank");
					}else{
						bootbox.alert("跳转失败,请稍后重试!");
					}
				},
				error:function(){
					bootbox.dialog({  
		                message: "请重新登陆！",  
		                title: "提示",  
		                buttons: {  
		                    OK: {  
		                        label: "OK",  
		                        className: "btn-primary",  
		                        callback: function () {  
		                            window.location.href="${ctx}/logout";
		                        }  
		                    }  
		                }  
		            });  
				}
			});
		},
		// 管理企业信息
		manage : function(companyId) {
			var url = "${ctx}/login/sysOutLogin?_t="+Math.random();
			$.ajax({
				url : url,
				type : 'post',
				dataType : 'json',
				data : {"companyId":companyId},
				success : function(data) {
					if(data!=null){
						var sign = data.sign;
						var timestamps = data.timestamps;
						// var newUrl = outPath+"login/sysOutLogin.action?_clientType=wap&sign="+sign+"&timestamps="+timestamps+"&companyId="+companyId+"&requestType=index";
						var newUrl = outPath+"#/jump?sign="+sign+"&timestamps="+timestamps+"&companyId="+companyId+"&requestType=index";
						if(win){
							win.close();
						}
						window.open(newUrl,"_blank");
					}else{
						bootbox.alert("跳转失败,请稍后重试!");
					}
				}
			});
		},
		roleManage : function(companyId) {
			if(is_submit){
				return;
			}
			is_submit=true;
			var title = '功能授权';
			var j = 0;
			base.getView({
				url : "${ctx}/moduleinfo/menuAuthority?companyId="+companyId,
				success : function(result) {
					bootbox.dialog({
						title : title,
						message : result,
						size : "large",
						closeButton : false,
						buttons : {
							cancel:{
								label:"取消",
								className:"btn btn-secondary",
								callback:function(){
									is_submit=false;
								}
							},
							sure : {
								label : "确定",
								className : "btn btn-primary",
								callback : function() {
									if (j != 0) {
										is_submit=false;
										return ;
									}
									j++;
									base.validAndAjaxSubmit($("#authorityForm"), $("#listForm"), function () {
										j = 0;
									});
									return false;
								}
							}
						}
					})
				}
			});
			/* var url = "${ctx}/login/sysOutLogin?_t="+Math.random();
			$.ajax({
				url : url,
				type : 'post',
				dataType : 'json',
				data : {"companyId" : companyId},
				success : function(data) {
					if(data != null){
						var sign = data.sign;
						var timestamps = data.timestamps;
						var newUrl = outPath + "login/sysOutLogin.action?_clientType=wap&sign="+sign+"&timestamps="+timestamps+"&companyId="+companyId+"&requestType=roleList";
						if(win){
							win.close();
						}
						win = window.open(newUrl,"_blank");
					}else{
						bootbox.alert("跳转失败,请稍后重试!");
					}
				}
			}); */
		},
		// 重置密码
		resetPwd : function(companyId) {
			if(is_submit){
				return;
			}
			is_submit=true;
			var title = '重置密码';
			var j = 0;
			base.getView({
				url:"${ctx}/company/resetPassWordJump?companyId="+companyId,
				success:function(result){
					bootbox.dialog({
						title:title,
						message:result,
						size:"large",
						closeButton : false,
						buttons:{
							cancel:{
								label:"取消",
								className:"btn btn-secondary",
								callback:function(){
									is_submit=false;
								}
							},
							sure:{
								label:"确定",
								className:"btn btn-primary",
								callback:function(){
									if (j != 0) {
										is_submit=false;
										return ;
									}
									j++;
									base.validAndAjaxSubmit($("#resetFormID"), $("#listForm"), function () {
										j = 0;
									});
									return false;
								}
							}
						}
					})
				}
			});
		}
	}
</script>
<#include "../common/footer.htm">